// pages/app/policyInfo/policyInfo.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    policyId: '',
    info: {},
    isFromShare: false
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      policyId: options.policyId
    })
    self.init();

    // var obj = wx.getLaunchOptionsSync();
    // console.log(obj);
    // if (obj.scene == 1043) {
      self.setData({
        isFromShare: true
      })
    // }
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/school/policyInfo';
    var params = {
      id: self.data.policyId
    };
    var success = function (res) {
      self.setData({
        info: res.info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  download(e) {
    wx.checkSession({
      success() {
        app.userInfoReadyCallback = function () {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {

              wx.request({
                url: host + '/lx/mini/school/countDownload',
                data: {
                  policyId: self.data.info.id
                }
              })

              var urls = e.currentTarget.dataset.urls;
              var list = urls.split(";");
              list.pop();

              for (var i = 0; i < list.length; i++) {
                var url = list[i];
                wx.showLoading({
                  title: '下载中…',
                  mask: true
                })
                wx.downloadFile({
                  url: url,
                  success(res) {
                    console.log(res);
                    var tempFilePath = res.tempFilePath;
                    wx.saveImageToPhotosAlbum({
                      filePath: tempFilePath,
                      success: function () {
                        wx.hideLoading();
                        wx.showToast({
                          title: '成功保存至相册',
                          icon: 'none'
                        })
                      },
                      fail: function () {
                        wx.hideLoading();
                      }
                    })
                  },
                  fail(res) {
                    wx.hideLoading();
                    console.log(res);
                    wx.showToast({
                      title: '下载失败',
                      icon: 'none'
                    })
                  }
                })
              }
            },
            fail(res) {
              console.log("authorize fail");
              console.log(res);
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '没有保存图片到系统相册的权限',
                confirmText: '去设置',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                    })
                  }
                }
              })
            }
          })
          app.userInfoReadyCallback = null;
        }

        app.login();
      },
      fail() {
        app.globalData.openId = null;
        app.serverLogin({
          success() {
            console.log('自动登录成功')
          },
          fail() {
            console.log('自动登录失败')
          }
        })
      }
    })
  },
  goHome(e) {
    wx.reLaunch({
      url: '/pages/app/schools/index/index'
    })
  }
})